<html>
<head>

<title>Groovy Goodness: Using Lists and Maps As Constructors</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Using Lists and Maps As Constructors</h3>

<div class="post">
<p>Constructors in Groovy can be invoked in a classic Java way, but we can also use lists or maps to create objects. Groovy supports an explicit coersion of a list to a constructor with the <code>as</code> keyword. Or we can rely on the implicit coersion when Groovy looks at the type of the variable to automatically convert the list to the right constructor call.</p>
<pre class="brush:groovy">
// Default constructor invocation:
def url1 = new URL('http', 'www.mrhaki.com', 80, '/')
assert 'http' == url1.protocol
assert 'www.mrhaki.com' == url1.host
assert 80 == url1.port
assert '/' == url1.file
assert '/' == url1.path

// Explicit coersion with as keyword:
def url2 = ['http', 'www.mrhaki.com', 80, '/'] as URL
assert 'http' == url1.protocol
assert 'www.mrhaki.com' == url2.host
assert 80 == url2.port
assert '/' == url2.file
assert '/' == url2.path

// Implicit coersion by type of variable:
URL url3 = ['http', 'www.mrhaki.com', 80, '/'] 
assert 'http' == url3.protocol
assert 'www.mrhaki.com' == url3.host
assert 80 == url3.port
assert '/' == url3.file
assert '/' == url3.path    
</pre>
<p>When we use <a href="http://mrhaki.blogspot.com/2009/08/groovy-goodness-groovybeans-simpler.html">GroovyBeans</a> syntax we can use a map with named parameter to invoke the constructor. But we can also use the explicit and implicit coersions Groovy provides.</p>
<pre class="brush:groovy">
// GroovyBean: Groovy creates a constructor
// that takes a map as parameter.
class Sample {
    Integer age 
    String name
}

def s1 = new Sample([age: 36, name: 'mrhaki'])
assert 36 == s1.age
assert 'mrhaki' == s1.name

// Explicit coersion with as keyword:
def s2 = [age: 36, name: 'mrhaki'] as Sample
assert 36 == s2.age
assert 'mrhaki' == s2.name

// Implicit coersion (by type of variable):
Sample s3 = [age: 36, name: 'mrhaki']
assert 36 == s3.age
assert 'mrhaki' == s3.name
</pre
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>